/**
 *
 */

export default class Mixin{
    private _mixin: any;

    private errorCode: string;

    get mixin(): any {
        return this._mixin;
    }

    set mixin(value: any) {
        this._mixin = value;
    }

    constructor() {
        let cl = this;
        this.mixin = {
            data: function () {
                return {
                    superCreate: true,
                    formName: "account",
                    /**
                     * 表单提交的函数
                     */
                    submitFunction: null,

                    menu: JSON.parse(sessionStorage.getItem("menu") == (null || undefined || "undefined") ? "{}" : sessionStorage.getItem("menu")),
                    /**
                     * 筛选的数据
                     */
                    search_data: {},
                    /**
                     * 新增的数据
                     */
                    insert_data: {},
                    /**
                     *
                     */
                    insert_data_rules: {},
                    /**
                     * 删除的数据
                     */
                    deletes_data: [],
                    /**
                     * 选择的数据
                     */
                    select_data: [],
                    /**
                     * 列表数据
                     */
                    list: [],
                    /**
                     * 总条数
                     */
                    total: 0,
                    /**
                     * 请求加载
                     */
                    listLoading: false,
                    /**
                     * 当前接口对象
                     */
                    baseController: cl,
                    /**
                     * 当前页数
                     */
                    currentPage: 1,
                    /**
                     * 显示的条数数集合
                     */
                    page_sizes: [5, 10, 20, 50, 100],
                    /**
                     * 显示的条数
                     */
                    page_size: 10,
                    /**
                     * 对话框是否可见
                     */
                    dialogVisible: false,
                    /**
                     * 时间
                     */
                    dataTime: []
                }

            },
            methods: {
                click(ev) {
                    eval(ev);
                },
                /**
                 * 删除
                 * @param cb 删除成功的 回调
                 */
                deletes(cb) {
                    this.baseController.deletes(this.deletes_data).then(response => {
                        this.$message({
                            message: response.message,
                            type: 'success'
                        });
                        this.select_data = [];
                        if (typeof (cb) == "string") {
                            eval(cb);
                        } else if (typeof (cb) == "function") {
                            cb(response);
                        } else {
                            this.queryManagerListPage({});
                        }
                    });
                },
                /**
                 * 修改
                 * @param cb
                 */
                update(data) {
                    let ret = this.baseController.update(data == null ? this.insert_data : data);
                    ret.then(response => {
                        this.$message({
                            message: response.message,
                            type: 'success'
                        });
                        this.queryManagerListPage();
                        return response;
                    });
                    return ret;
                },
                /**
                 * 新增
                 * @param cb
                 */
                insert(cb) {
                    if (Object.keys(this.insert_data).length == 0) {
                        this.$message.error("添加的数据不能为空");
                        return;
                    }
                    let ret = this.baseController.insert(this.insert_data);
                    ret.then(response => {
                        this.$message({
                            message: response.message,
                            type: 'success'
                        });
                        if (typeof (cb) == "string") {
                            eval(cb);
                        } else if (typeof (cb) == "function") {
                            cb(response);
                        } else {
                            this.queryManagerListPage();
                        }
                        return response;
                    });
                    return ret;
                },
                /**
                 * 打开新增框
                 */
                openInsert(f) {
                    this.$refs[(f = (f == null ? this.formName : f))].openWindow({});
                    this.f = f;
                    this.submitFunction = this.insert;
                },
                /**
                 * 打开修改框
                 */
                opennUpdate(f) {
                    if (this.select_data.length != 1) {
                        this.$message({
                            type: 'warning',
                            message: '请选择一条数据'
                        });
                        return;
                    }
                    this.$refs[(f = (f == null ? this.formName : f))].openWindow(this.select_data[0]);
                    this.f = f;
                    this.submitFunction = this.update;
                },
                /**
                 * 修改状态
                 */
                updateStatus(data) {
                    if (this.select_data.length != 1) {
                        this.$message({
                            type: 'warning',
                            message: '请选择一条数据'
                        });
                        return;
                    }
                    data = Object.assign(data, {id: this.select_data[0].id});
                    let ret = this.baseController.update(data);
                    ret.then(response => {
                        this.$message({
                            message: response.message,
                            type: 'success'
                        });
                        this.flush({});
                        return response;
                    });
                    return ret;
                },
                /**
                 * 刷新
                 * @param data
                 * @returns {*}
                 */
                flush(data, page, page_size) {
                    return this.baseController.queryManagerListPage(data == null ? {} : data, page == null ? this.currentPage : page, page_size == null ? this.page_size : page_size);
                },
                /**
                 * 分页查询
                 */
                queryManagerListPage(data, cb) {
                    this.flush(Object.assign(data == null ? {} : data, this.search_data), this.currentPage, this.page_size).then(response => {
                        this.list = response.data.rows;
                        this.total = response.data.total;
                        if (typeof (cb) == "string") {
                            eval(cb);
                        } else if (typeof (cb) == "function") {
                            cb(response);
                        }
                        return response;
                    });
                },

                /**
                 * 不分页查询
                 */
                query(data, cb) {
                    data = (data == null ? {} : data);
                    this.baseController.query(Object.assign(data, this.search_data)).then(response => {
                        this.list = response.data;
                        if (typeof (cb) == "string") {
                            eval(cb);
                        } else if (typeof (cb) == "function") {
                            cb(response);
                        }
                        return response;
                    });
                },
                /**
                 * 选择列表事件触发
                 * @param val
                 */
                selectionChange(val) {
                    this.select_data = val;
                },
                /**
                 * 搜索
                 * @param s
                 */
                search(data) {
                    if (this.dataTime != null && (this.dataTime.time != null && this.dataTime.time.length > 0)) {
                        this.search_data.endTime = this.dataTime.time[1];
                        this.search_data.startTime = this.dataTime.time[0];
                    } else {
                        delete this.search_data.startTime;
                        delete this.search_data.endTime;
                    }
                    this.queryManagerListPage(Object.assign((data == null ? {} : data), this.search_data));
                },
                /**
                 * 选择显示的条数
                 * @param val 当前显示的条数
                 */
                handleSizeChange(val) {
                    this.page_size = val;
                    this.queryManagerListPage();
                },
                /**
                 * 分页
                 * @param val 当前页
                 */
                handleCurrentChange(val) {
                    this.currentPage = val;
                    this.queryManagerListPage();
                },
                /**
                 * 打开询问消息对话框
                 * @param cb
                 */
                openDeleteMessage(cb) {
                    if (this.select_data.length == 0) {
                        this.$message.error("选择一条要删除的数据");
                        return;
                    }
                    let this_ = this;
                    this.deletes_data = [];
                    this.select_data.forEach(res => {
                        this.deletes_data.push(res.id);
                    })
                    this.$confirm('此操作将永久删除' + (this.deletes_data.length) + '数据, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        center: true
                    }).then(() => {
                        this_.deletes(cb);
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            }
        };
    }
}
